<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.23.
   家居公司优惠活动模型($$)*/

!defined('Q_PATH') && exit;

class JiajueventModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'jiajuevent',
			'check'=>array(
				'jiajuevent_sort'=>array(
					array('number',Q::L('序号只能是数字','__COMMON_LANG__@Common')),
				),
				'jiajuevent_title'=>array(
					array('require',Q::L('活动标题不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('活动标题不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajuevent_thumb'=>array(
					array('require',Q::L('活动缩略图不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('活动缩略图不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajuevent_description'=>array(
					array('max_length',250,Q::L('活动描述不能超过250个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	protected function beforeSave_(){
		$this->jiajuevent_title=C::text($this->jiajuevent_title);
		$this->jiajuevent_description=C::text($this->jiajuevent_description);
		$this->jiajuevent_thumb=C::text($this->jiajuevent_thumb);
		$this->jiajuevent_username=C::text($this->jiajuevent_username);
		$this->jiajuevent_editusername=C::text($this->jiajuevent_editusername);
		
		if($this->jiajuevent_sort<0){
			$this->jiajuevent_sort=0;
		}
		if($this->jiajuevent_sort>999){
			$this->jiajuevent_sort=999;
		}
	}

	public function saveData($arrCompany){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		$this->jiajuevent_starttime=strtotime(Q::G('jiajuevent_starttime'));
		$this->jiajuevent_endtime=strtotime(Q::G('jiajuevent_endtime'));

		if(empty($arrCompany['jiajucompany_id'])){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',$arrCompany)->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
		}

		// 商家&城市
		$this->jiajucompany_id=$arrCompany['jiajucompany_id'];
		$this->site_id=$arrCompany['site_id'];
		$this->province_id=$arrCompany['province_id'];
		$this->city_id=$arrCompany['city_id'];

		// 添加人
		$this->user_id=$GLOBALS['___login___']['user_id'];
		$this->jiajuevent_username=$GLOBALS['___login___']['user_name'];
		$this->jiajuevent_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajuevent_edituserid=$GLOBALS['___login___']['user_id'];
		$this->save();
		if($this->isError()){
			$bRollback=true;
		}

		// 保存活动介绍
		if($bRollback===false){
			$oJiajueventcontent=new JiajueventcontentModel();
			$oJiajueventcontent->jiajuevent_id=$this->jiajuevent_id;
			$oJiajueventcontent->save();
			if($oJiajueventcontent->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajueventcontent->getErrorMessage();
			}
		}

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

	public function updateData($arrCompany){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		$this->jiajuevent_starttime=strtotime(Q::G('jiajuevent_starttime'));
		$this->jiajuevent_endtime=strtotime(Q::G('jiajuevent_endtime'));

		if(empty($arrCompany['jiajucompany_id'])){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',$arrCompany)->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
		}
		
		// 商家&城市
		$this->jiajucompany_id=$arrCompany['jiajucompany_id'];
		$this->site_id=$arrCompany['site_id'];
		$this->province_id=$arrCompany['province_id'];
		$this->city_id=$arrCompany['city_id'];

		// 编辑人
		$this->jiajuevent_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajuevent_edituserid=$GLOBALS['___login___']['user_id'];

		// 修改后提交
		if($this->jiajuevent_status==1 && APP_NAME!=='admin'){
			$this->jiajuevent_status=11;
		}
		$this->save('update');
		if($this->isError()){
			$bRollback=true;
		}

		// 保存活动内容
		if($bRollback===false){
			$oJiajueventcontent=JiajueventcontentModel::F('jiajuevent_id=?',$this->jiajuevent_id)->getOne();
			if(empty($oJiajueventcontent['jiajuevent_id'])){
				$bRollback=true;
			}else{
				$oJiajueventcontent->save('update');
				if($oJiajueventcontent->isError()){
					$bRollback=true;
					$this->_sErrorMessage=$oJiajueventcontent->getErrorMessage();
				}
			}
		}

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

}
